package com.sinosoft.antiques.service;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.sinosoft.antiques.common.Common;
import com.sinosoft.commontools.record.Record;
import com.sinosoft.dbtools.operate.DBActorMgr;
import com.sinosoft.dbtools.operate.IDBActor;
import com.sinosoft.dbtools.operate.single.DBSingleTableActorMgr;
import com.sinosoft.dbtools.operate.single.IDBSingleTableActor;

public class BasicService {

	public static final Logger logger = LogManager
			.getLogger(BasicService.class);

	public static IDBSingleTableActor getSingleTableActor(String tableName) {
		IDBSingleTableActor actor = DBSingleTableActorMgr
				.getDBSingleTableActor(tableName);
		return actor;
	}

	public static IDBActor getMySqlDBActor() {
		IDBActor actor = DBActorMgr.getDBActor("oraclepool");
		// IDBActor actor = DBActorMgr.getDBActor("mysqlpool");
		return actor;
	}

	public static synchronized long getNIMAGEMaxId() {
		long arg0 = -1L;
		IDBActor arg2 = getMySqlDBActor();
		try {
			String arg3 = arg2
					.getSingleFldStrValue("select N_IMAGE_SEQ.nextval as num from dual");
			arg0 = Long.parseLong(arg3);
		} catch (Exception arg4) {
			System.out.println("获取序列器错误：" + arg4.getMessage()
					+ "\t 序列名=N_IMAGE_SEQ");
			arg4.printStackTrace();
		}

		return arg0;
	}

	public static synchronized long getLSYNCMaxId() {
		long arg0 = -1L;
		IDBActor arg2 = getMySqlDBActor();
		try {
			String arg3 = arg2
					.getSingleFldStrValue("select L_SYNC_SEQ.nextval as num from dual");
			arg0 = Long.parseLong(arg3);
		} catch (Exception arg4) {
			System.out.println("获取序列器错误：" + arg4.getMessage()
					+ "\t 序列名=L_SYNC_SEQ");
			arg4.printStackTrace();
		}

		return arg0;
	}
	
	public static synchronized long getIMAGETASKMaxId() {
		long arg0 = -1L;
		IDBActor arg2 = getMySqlDBActor();
		try {
			String arg3 = arg2
					.getSingleFldStrValue("select SEQ_IMAGE_TASK.nextval as num from dual");
			arg0 = Long.parseLong(arg3);
		} catch (Exception arg4) {
			System.out.println("获取序列器错误：" + arg4.getMessage()
					+ "\t 序列名=SEQ_IMAGE_TASK");
			arg4.printStackTrace();
		}

		return arg0;
	}
	
	public static List<Record> getTask(){
		IDBActor arg2 = getMySqlDBActor();
		try {
			String sql =  "select * from IMAGE_TASK where FLAGSTATUS='1'";
			List<Record> arg3 = arg2.getRowsForRecord(sql);
			return arg3;
		} catch (Exception arg4) {
			System.out.println("获取序列器错误：" + arg4.getMessage()
					+ "\t 序列名=SEQ_IMAGE_TASK");
			arg4.printStackTrace();
		}

		return null;
	}
	
	public static boolean updateTask(int id, String flagstatus) {
		String updateSql = "update IMAGE_TASK set FLAGSTATUS='" + flagstatus + "' where id=" + id;
		try {
			int rs = getMySqlDBActor().ExeSql(updateSql);
			if (rs == 1) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean insertTask(String userid, String filepath) {
		long id = getIMAGETASKMaxId();
		String insertSql = "insert INTO IMAGE_TASK(ID,FILEPATH,FLAGSTATUS,USERID)"
				+ "VALUES ("+id+",'"+filepath+"','1',"+userid+")";
		try {
			int rs = getMySqlDBActor().ExeSql(insertSql);
			if (rs == 1) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static Record wrapSuccessSingleResult(Record record, Object data) {
		record.add("status", Common.ReqOK);
		record.add("data", data);

		return record;
	}

	public static void doUpload_(String uploadFilePath, String fdir,
			String fileName, Record data, String fileField) {
		File uploadFile = new File(uploadFilePath);
		File filedir = new File(fdir);
		if (!filedir.exists()) {
			// 创建目标文件夹
			filedir.mkdirs();
		}
		String nfilepath = fdir + File.separator + fileName;
		File nfile = new File(nfilepath);
		DataInputStream in = null;
		DataOutputStream out = null;
		try {
			in = new DataInputStream(new FileInputStream(uploadFile));
			out = new DataOutputStream(new FileOutputStream(nfile));
			byte[] buffer = new byte[4096];
			int count = 0;
			while ((count = in.read(buffer)) > 0) {
				out.write(buffer, 0, count);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return;
	}

	public static Record upload_mass(String uploadFilePath, String filename,
			Record data, String fdir) {
		Record record = new Record();
		Record info = new Record();
		try {
			// String path250 = ImageProcess.zoom(uploadFilePath, 250);
			// if(path250==null||path250.equals(""))
			// return wrapFailResult(record, "文件读写错误");
			// String path100 = ImageProcess.zoom(uploadFilePath, 100);

			doUpload_(uploadFilePath, fdir, filename, data, "yxdz");
			// if(path250!=null&&!"".equals(path250))
			// doUpload_(path250, dbPoolPath, filename, data, "lldz");
			// if(path100!=null&&!"".equals(path100))
			// doUpload_(path100, dbPoolPath, filename, data, "sldz");

			info.add("yxmc", filename);
			info.add("yxdz", data.getStrValue("yxdz"));
			info.add("filesize", data.getStrValue("filesize"));
			// if(path100!=null&&!"".equals(path100))
			// info.add("sldz", data.getStrValue("sldz"));
			// else
			// info.add("sldz", data.getStrValue("yxdz"));
			// if(path250!=null&&!"".equals(path250))
			// info.add("lldz", data.getStrValue("lldz"));
			// else
			info.add("lldz", data.getStrValue("yxdz"));

			// 删除本地临时文件
			// FileUtil.deleteFile(path250);
			// FileUtil.deleteFile(path100);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return wrapSuccessSingleResult(record, info);
	}
}
